package com.intellagric.agriecom.service.agriecom_produce_comment;

import com.github.pagehelper.PageHelper;
import com.intellagric.agriecom.module.agriecom_produce_comment.ProduceCommentService;
import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.dao.agriecom.agriecom_produce_comment.AgriecomProduceCommentDao;
import com.intellagric.mapper.AgriecomProduceCommentMapper;
import com.intellagric.mapper.AgriecomProduceMapper;
import com.intellagric.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProduceCommentServiceImpl implements ProduceCommentService {

    @Autowired
    private AgriecomProduceCommentMapper produceCommentMapper;

    @Autowired
    private AgriecomProduceCommentDao agriecomProduceCommentDao;

    @Autowired
    private AgriecomProduceMapper agriecomProduceMapper;

    @Override
    public ResponseMessage addProduceComment(AgriecomProduceComment produceComment) {
        if(produceCommentMapper.insertSelective(produceComment)==1)
            return ResponseMessage.success();
        else
            return ResponseMessage.fail();

    }

    @Override
    public ResponseMessage deleteProduceComment(String id) {
        if(produceCommentMapper.deleteByPrimaryKey(id)==1)
            return ResponseMessage.success();
        else
            return ResponseMessage.fail();
    }

    @Override
    public ResponseMessage editProduceComment(AgriecomProduceComment quotation) {
        if(produceCommentMapper.updateByPrimaryKeyWithBLOBs(quotation)==1)
            return ResponseMessage.success();
        else
            return ResponseMessage.fail();
    }

    @Override
    public AgriecomProduceComment getProduceCommentById(String id) {
        return produceCommentMapper.selectByPrimaryKey(id);
    }

    @Override
    public LayuiDataGridResult getProduceCommentList() {
        AgriecomProduceCommentExample produceCommentExample=new AgriecomProduceCommentExample();
        produceCommentExample.createCriteria().andIdIsNotNull();
        produceCommentExample.setOrderByClause("comment_time desc");
        List<AgriecomProduceComment> agriecomProduceCommentMapperList = produceCommentMapper.selectByExampleWithBLOBs(produceCommentExample);


        return LayuiDataGridResult.success().add(agriecomProduceCommentMapperList,agriecomProduceCommentMapperList.size());
    }

    @Override
    public LayuiDataGridResult getProduceCommentList(int page, int rows) {
        PageHelper.startPage(page,rows);


        return getProduceCommentList();
    }

    @Override
    public ResponseMessage batchDeleteProduceComment(String[] ids) {


        if (agriecomProduceCommentDao.batchDeleteByPrimaryKey(ids) <1)
            return ResponseMessage.fail();
        return ResponseMessage.success();
    }

    @Override
    public LayuiDataGridResult getCommentByKeyword(int page, int limit, String keyword) {
        PageHelper.startPage(page, limit);


        AgriecomProduceExample produceExample=new AgriecomProduceExample();
        produceExample.createCriteria().andProduceNameLike("%"+keyword+"%");
        List<AgriecomProduce> produceList=agriecomProduceMapper.selectByExampleWithBLOBs(produceExample);
        List<String> ids=new ArrayList<>();
        for(int i=0;i<produceList.size();i++){
                   ids.add(produceList.get(i).getProduceId());
               }

        AgriecomProduceCommentExample commentExample=new AgriecomProduceCommentExample();

        commentExample.createCriteria().andProduceIdIn(ids);

        List<AgriecomProduceComment> commentList=produceCommentMapper.selectByExampleWithBLOBs(commentExample);

        return LayuiDataGridResult.success().add(commentList,commentList.size() );

    }


}
